﻿using System.Collections.Generic;
using System.Configuration;
using System.Web.Mvc;
using KMF.Common;
using KMF.DAL;
using KMF.WebUI.Areas.KMFadmin.Attributes;
using KMF.WebUI.Areas.KMFadmin.CustomHelpers;
using KMF.WebUI.Areas.KMFadmin.Models;

namespace KMF.WebUI.Areas.KMFadmin.Controllers
{
    [AdminLoggedAuthorize]
    public class AdminNewsCategoryController : Controller
    {
        //
        // GET: /NewCategory/

        public ActionResult Index(int page = 1)
        {
            var itemsPerPage = int.Parse(ConfigurationManager.AppSettings["DefaultItemsPerPage"]);
            var totalPage = 0;
            var totalRecord = 0;
            var model = NewsCategoryDb.GetAllParentListAdmin(page, itemsPerPage, ref totalPage, ref totalRecord) ?? new List<NewsCategoryListAdminDto>();

            ViewBag.PageInfo = new PageInfo { CurrentPage = page, PageSize = itemsPerPage, TotalPages = totalPage };
            ViewBag.TotalRecord = totalRecord;
            return View(model);
        }

        public ActionResult AddNewsCategory()
        {
            ViewBag.ParentList = FunctionHelper.GetMenuListThreeLevelNews() ?? new List<NewsCategoryDto>();
            return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult AddNewsCategory(NewsCategoryDto model)
        {
            NewsCategoryDb.Add(model);
            return RedirectToAction("Index", "AdminNewsCategory");
        }

        public ActionResult EditNewsCategory(int id)
        {
            ViewBag.ParentList = FunctionHelper.GetMenuListThreeLevelNews() ?? new List<NewsCategoryDto>();
            var model = NewsCategoryDb.GetById(id);
            return View(model);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult EditNewsCategory(NewsCategoryDto model)
        {
            NewsCategoryDb.Update(model);
            return RedirectToAction("Index", "AdminNewsCategory");
        }

        public JsonResult DeleteNewsCategory(int id)
        {
            var result = NewsCategoryDb.Delete(id);
            var data = new { id, result };
            return Json(data, JsonRequestBehavior.AllowGet);
        }
    }
}
